package com.glela.controller;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.glela.api.upload.qiniu.general.QiniuUtils;
import com.glela.common.ImgBasePathRequest;
import com.glela.common.util.Base64;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.util.ParameterUtils;
import com.glela.serial.model.response.ResponseTalentSerialContentVo;
import com.glela.serial.model.response.ResponseTalentSerialVo;
import com.glela.serial.service.TalentSerialService;
@RequestMapping("/TalentSerial")
@Controller
public class TalentSerialController {
	private static Logger logger = LoggerFactory.getLogger(SerialController.class);
	@Autowired
	private TalentSerialService talentSerialService;
	
	/**
	 * 上传头像（Base64位）
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/imgBasePath", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> imgBasePath(@RequestBody ImgBasePathRequest request) {
		try {
			// 验证传入的图片后缀和图片的base64 
			if (ParameterUtils.validParams(request.getImgBase())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (ParameterUtils.validParams(request.getSuffix())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			byte[] b = Base64.decode(request.getImgBase());
			//图片放在七牛上，并返回图片在七牛上的地址
			String imgurl = QiniuUtils.uploadByBytys64("file/talentSerial/", b, request.getSuffix());
			if ("".equals(imgurl)) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29907), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<String>(imgurl), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("上传图片异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 上传头像
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/imguploading", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> imguploading(@RequestBody ImgBasePathRequest request) {
		try {
			// 验证图片路径
			if (ParameterUtils.validParams(request.getUrlPath())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			MultipartFile imgfile = multipartRequest.getFile(request.getUrlPath());
			// 上传图片到七牛
			String imgurl = QiniuUtils.upload("file/talentSerial/", imgfile);
			if ("".equals(imgurl)) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29907), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<String>(imgurl), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("上传图片异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 图片删除
	 * @param request    urlPath 删除图片文件
	 * @param response
	 * @param data
	 */
	@RequestMapping(value = "/imgdelete", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> imgdelete(@RequestBody ImgBasePathRequest request) {
		try {
			if (ParameterUtils.validParams(request.getUrlPath())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			boolean imgurl = QiniuUtils.delete(request.getUrlPath());
			if (imgurl==false) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29911), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("上传图片异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 生成达人期
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/addTalentSerial", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addTalentSerial(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			// 验证达人ID
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			ResponseTalentSerialVo rs=talentSerialService.addTalentSerial(responseTalentSerialVo.getTalentId());
			
			return new ResponseEntity<>(new ResponseResultModel<ResponseTalentSerialVo>(rs), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("生成达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 添加达人期内商品
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/addTalentSerialGoods", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addTalentSerialGoods(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			// 验证达人期ID和商品ID
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.addTalentSerialGoods(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getGoodId());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20142), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("添加达人期内商品异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 删除达人期
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/removeTalentSerial", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> removeTalentSerial(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getTalentId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.removeTalentSerial(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getTalentId());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20144), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 删除期内商品
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/removeTalentSerialGoods", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> removeTalentSerialGoods(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getGoodId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.removeTalentSerialGoods(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getGoodId());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20144), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 上传期封面
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/changeTalentSerialImagePath", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> changeTalentSerialImagePath(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			// 达人期ID 图片地址不为空
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(),responseTalentSerialVo.getImagePath())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.changeTalentSerialImagePath(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getImagePath());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20143), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 上传期别名
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/updateTalentSerialLabelName", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> updateTalentSerialLabelName(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getLabelName())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.updateTalentSerialLabelName(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getLabelName());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20143), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("上传期别名异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 上传期标题
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/updateTalentSerialLaTitle", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> updateTalentSerialLaTitle(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getTitle())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.updateTalentSerialLaTitle(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getTitle());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20143), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("上传期标题异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 发布达人期
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/addTalentSerialEditAndShare", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addTalentSerialEditAndShare(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentId(), responseTalentSerialVo.getTalentSerialId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			//插入信息到达人用户期编辑与分享表中 并更新达人用户期咨询管理表的标题
			int i=talentSerialService.addTalentSerialEditAndShare(responseTalentSerialVo.getTalentId(), responseTalentSerialVo.getTalentSerialId(),responseTalentSerialVo.getTitle());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20143), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 添加期详情图片
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/addTalentSerialCtntent", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> addTalentSerialCtntent(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getImagePathlist())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.addTalentSerialCtntent(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getImagePathlist()
					,responseTalentSerialVo.getImgwidth(),responseTalentSerialVo.getImgheight());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20143), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	/**
	 * 删除期详情图片
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/removeTalentSerialontent", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> removeTalentSerialontent(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId(), responseTalentSerialVo.getTalentSerialontentId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			int i=talentSerialService.removeTalentSerialontent(responseTalentSerialVo.getTalentSerialId(),responseTalentSerialVo.getTalentSerialontentId());
			if (i==0) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20144), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("删除达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 查询期内图片
	 * 
	 * @param requestUserSet
	 * @return
	 */
	@RequestMapping(value = "/listTalentSerialContentImgPath", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> listTalentSerialContentImgPath(@RequestBody ResponseTalentSerialVo responseTalentSerialVo) {
		try {
			if (ParameterUtils.validParams(responseTalentSerialVo.getTalentSerialId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			List<ResponseTalentSerialContentVo> rs=talentSerialService.listTalentSerialContentImgPath(responseTalentSerialVo.getTalentSerialId());
			
			return new ResponseEntity<>(new ResponseResultModel<List<ResponseTalentSerialContentVo>>(rs), HttpStatus.OK);
		} catch (Exception e) {
			logger.error("生成达人期异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	
}
